package com.yiren.backstage.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.yiren.backstage.currency.entity.SiteEntity;

import com.yiren.backstage.currency.entity.LineInfoEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "line_entity".
*/
public class LineInfoEntityDao extends AbstractDao<LineInfoEntity, String> {

    public static final String TABLENAME = "line_entity";

    /**
     * Properties of entity LineInfoEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property MapId = new Property(1, String.class, "mapId", false, "MAP_ID");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
        public final static Property SiteCode = new Property(3, String.class, "siteCode", false, "SITE_CODE");
        public final static Property UserId = new Property(4, int.class, "userId", false, "USER_ID");
        public final static Property LineVer = new Property(5, float.class, "lineVer", false, "LINE_VER");
        public final static Property UseRange = new Property(6, int.class, "useRange", false, "USE_RANGE");
        public final static Property UpdateDetails = new Property(7, String.class, "updateDetails", false, "UPDATE_DETAILS");
        public final static Property UpdateMode = new Property(8, int.class, "updateMode", false, "UPDATE_MODE");
        public final static Property PublishTime = new Property(9, Long.class, "publishTime", false, "PUBLISH_TIME");
        public final static Property State = new Property(10, int.class, "state", false, "STATE");
        public final static Property LineType = new Property(11, int.class, "lineType", false, "LINE_TYPE");
        public final static Property PartneModifyVal = new Property(12, String.class, "partneModifyVal", false, "PARTNE_MODIFY_VAL");
        public final static Property CoachModifyVal = new Property(13, String.class, "coachModifyVal", false, "COACH_MODIFY_VAL");
        public final static Property Location = new Property(14, String.class, "location", false, "LOCATION");
        public final static Property Longitude = new Property(15, double.class, "longitude", false, "LONGITUDE");
        public final static Property Latitude = new Property(16, double.class, "latitude", false, "LATITUDE");
        public final static Property PointNumber = new Property(17, int.class, "pointNumber", false, "POINT_NUMBER");
    }

    private DaoSession daoSession;

    private Query<LineInfoEntity> mapEntity_LinesQuery;

    public LineInfoEntityDao(DaoConfig config) {
        super(config);
    }
    
    public LineInfoEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, LineInfoEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String mapId = entity.getMapId();
        if (mapId != null) {
            stmt.bindString(2, mapId);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
 
        String siteCode = entity.getSiteCode();
        if (siteCode != null) {
            stmt.bindString(4, siteCode);
        }
        stmt.bindLong(5, entity.getUserId());
        stmt.bindDouble(6, entity.getLineVer());
        stmt.bindLong(7, entity.getUseRange());
 
        String updateDetails = entity.getUpdateDetails();
        if (updateDetails != null) {
            stmt.bindString(8, updateDetails);
        }
        stmt.bindLong(9, entity.getUpdateMode());
 
        Long publishTime = entity.getPublishTime();
        if (publishTime != null) {
            stmt.bindLong(10, publishTime);
        }
        stmt.bindLong(11, entity.getState());
        stmt.bindLong(12, entity.getLineType());
 
        String partneModifyVal = entity.getPartneModifyVal();
        if (partneModifyVal != null) {
            stmt.bindString(13, partneModifyVal);
        }
 
        String coachModifyVal = entity.getCoachModifyVal();
        if (coachModifyVal != null) {
            stmt.bindString(14, coachModifyVal);
        }
 
        String location = entity.getLocation();
        if (location != null) {
            stmt.bindString(15, location);
        }
        stmt.bindDouble(16, entity.getLongitude());
        stmt.bindDouble(17, entity.getLatitude());
        stmt.bindLong(18, entity.getPointNumber());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, LineInfoEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String mapId = entity.getMapId();
        if (mapId != null) {
            stmt.bindString(2, mapId);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
 
        String siteCode = entity.getSiteCode();
        if (siteCode != null) {
            stmt.bindString(4, siteCode);
        }
        stmt.bindLong(5, entity.getUserId());
        stmt.bindDouble(6, entity.getLineVer());
        stmt.bindLong(7, entity.getUseRange());
 
        String updateDetails = entity.getUpdateDetails();
        if (updateDetails != null) {
            stmt.bindString(8, updateDetails);
        }
        stmt.bindLong(9, entity.getUpdateMode());
 
        Long publishTime = entity.getPublishTime();
        if (publishTime != null) {
            stmt.bindLong(10, publishTime);
        }
        stmt.bindLong(11, entity.getState());
        stmt.bindLong(12, entity.getLineType());
 
        String partneModifyVal = entity.getPartneModifyVal();
        if (partneModifyVal != null) {
            stmt.bindString(13, partneModifyVal);
        }
 
        String coachModifyVal = entity.getCoachModifyVal();
        if (coachModifyVal != null) {
            stmt.bindString(14, coachModifyVal);
        }
 
        String location = entity.getLocation();
        if (location != null) {
            stmt.bindString(15, location);
        }
        stmt.bindDouble(16, entity.getLongitude());
        stmt.bindDouble(17, entity.getLatitude());
        stmt.bindLong(18, entity.getPointNumber());
    }

    @Override
    protected final void attachEntity(LineInfoEntity entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public LineInfoEntity readEntity(Cursor cursor, int offset) {
        LineInfoEntity entity = new LineInfoEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // mapId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // siteCode
            cursor.getInt(offset + 4), // userId
            cursor.getFloat(offset + 5), // lineVer
            cursor.getInt(offset + 6), // useRange
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // updateDetails
            cursor.getInt(offset + 8), // updateMode
            cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9), // publishTime
            cursor.getInt(offset + 10), // state
            cursor.getInt(offset + 11), // lineType
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // partneModifyVal
            cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // coachModifyVal
            cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // location
            cursor.getDouble(offset + 15), // longitude
            cursor.getDouble(offset + 16), // latitude
            cursor.getInt(offset + 17) // pointNumber
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, LineInfoEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setMapId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setSiteCode(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setUserId(cursor.getInt(offset + 4));
        entity.setLineVer(cursor.getFloat(offset + 5));
        entity.setUseRange(cursor.getInt(offset + 6));
        entity.setUpdateDetails(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setUpdateMode(cursor.getInt(offset + 8));
        entity.setPublishTime(cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9));
        entity.setState(cursor.getInt(offset + 10));
        entity.setLineType(cursor.getInt(offset + 11));
        entity.setPartneModifyVal(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setCoachModifyVal(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setLocation(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
        entity.setLongitude(cursor.getDouble(offset + 15));
        entity.setLatitude(cursor.getDouble(offset + 16));
        entity.setPointNumber(cursor.getInt(offset + 17));
     }
    
    @Override
    protected final String updateKeyAfterInsert(LineInfoEntity entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(LineInfoEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(LineInfoEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "lines" to-many relationship of MapEntity. */
    public List<LineInfoEntity> _queryMapEntity_Lines(String mapId) {
        synchronized (this) {
            if (mapEntity_LinesQuery == null) {
                QueryBuilder<LineInfoEntity> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.MapId.eq(null));
                mapEntity_LinesQuery = queryBuilder.build();
            }
        }
        Query<LineInfoEntity> query = mapEntity_LinesQuery.forCurrentThread();
        query.setParameter(0, mapId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getSiteEntityDao().getAllColumns());
            builder.append(" FROM line_entity T");
            builder.append(" LEFT JOIN site_entity T0 ON T.\"SITE_CODE\"=T0.\"SITE_CODE\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected LineInfoEntity loadCurrentDeep(Cursor cursor, boolean lock) {
        LineInfoEntity entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        SiteEntity siteEntity = loadCurrentOther(daoSession.getSiteEntityDao(), cursor, offset);
        entity.setSiteEntity(siteEntity);

        return entity;    
    }

    public LineInfoEntity loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<LineInfoEntity> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<LineInfoEntity> list = new ArrayList<LineInfoEntity>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<LineInfoEntity> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<LineInfoEntity> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
